﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Model;

namespace Web.Admin
{
    public partial class Article_Add : BaseCode.BaseAdmin
    {
        private DAL_Article dalArticle = new DAL_Article();
        private DAL_Tags dalTags = new DAL_Tags();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TagList();
            }
        }
        public void TagList()
        {
            DataTable list = dalTags.GetTagByType(2);
            for (int i = 0; i < list.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Value = list.Rows[i]["id"].ToString();
                li.Text = list.Rows[i]["Name"].ToString();
                li.Attributes.Add("ToopTip", "'" + list.Rows[i]["id"].ToString() + "'");
                this.cblTaglist.Items.Add(li);
            }

            DataTable list2 = dalTags.GetTagByType(1);
            for (int j = 0; j < list2.Rows.Count; j++)
            {
                ListItem li = new ListItem();
                li.Value = list2.Rows[j]["id"].ToString();
                li.Text = list2.Rows[j]["Name"].ToString();
                li.Attributes.Add("ToopTip", "'" + list2.Rows[j]["id"].ToString() + "'");
                this.cblTypelist.Items.Add(li);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string name = this.txtName.Text;
            string ename = this.txtEname.Text;
            //string userid = Session["UserID"].ToString();
            string keyword = this.txtKeywords.Text;
            string author = string.Empty;
            string sourse = string.Empty;
            string sourseurl = string.Empty;
            string contents = this.hiddenContent.Value;
            DateTime publishtime = DateTime.Parse(this.txtPublishTime.Text);
            
            int state = (this.rbtnNoUse.Checked == true) ? 0 : 1;


            Model_Article article = new Model_Article()
            {
                name = name,
                ename = ename,
                keywords = keyword,
                state = state,
                sourse = sourse,
                sourseurl = sourseurl,
                author = author,
                contents = contents,
                commentCount = 0,
                hitCount = 0,
                user_id = 1,//int.Parse(userid),
                lastHitTime = DateTime.Now,
                publishTime = publishtime,
                updatetime = DateTime.Now,
                addtime = DateTime.Now
            };
            int artid = dalArticle.AddArticle(article);
            if (artid != 0)
            {
                for (int i = 0; i < this.cblTaglist.Items.Count; i++)
                {
                    if (this.cblTaglist.Items[i].Selected == true)
                    {
                        dalArticle.AddTag_Art(int.Parse(this.cblTaglist.Items[i].Value), artid);
                    }
                }
                for (int i = 0; i < this.cblTypelist.Items.Count; i++)
                {
                    if (this.cblTypelist.Items[i].Selected == true)
                    {
                        dalArticle.AddTag_Art(int.Parse(this.cblTypelist.Items[i].Value), artid);
                    }
                }
                Page.ClientScript.RegisterStartupScript(typeof(string), "提示", "<script>alert('添加成功！');</script>");
                Response.Redirect("Article.aspx");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(typeof(string), "提示", "<script>alert('添加失败！');</script>");
                return;
            }
           
        }
    }
}